﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Entities.Modules;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Drawing;

namespace Photos
{
    public class ModuleCompanyBase : PortalModuleBase
    {
        private PhotosDataContext dB = null;

        public string PathUrlImage
        {

            get
            {
                return "~/images/photos/";
            }
        }



        public string PathPhysicalImage
        {

            get
            {
                return Server.MapPath("~/images/photos/");
            }
        }

        public PhotosDataContext DB
        {
            get
            {
                if (dB == null)
                {
                    dB = new PhotosDataContext();
                }
                return dB;
            }

        }

        public string ReWriteUrl(string id, string title)
        {

            //string url = EditUrl("", "", "", "newsid=" + id, "detail=" + TabId.ToString());
            string url = DotNetNuke.Common.Globals.NavigateURL(TabId, "", "detail=" + TabId, "newsid=" + id, "title=" + ReWriteString(title));
            return url;
        }

        public string ReWriteString(string str)
        {
            StringBuilder sb = new StringBuilder();
            str = Regex.Replace(str, "Đ|đ|&#273;|&#272;", "d", RegexOptions.IgnoreCase);
            str = str.Normalize(NormalizationForm.FormKD);
            for (int i = 0; i < str.Length; i++)
            {
                if (char.IsWhiteSpace(str[i])) sb.Append('-');
                else if (char.GetUnicodeCategory(str[i]) != UnicodeCategory.NonSpacingMark
                    && !char.IsPunctuation(str[i])
                    && !char.IsSymbol(str[i]))
                    sb.Append(str[i]);
            }
            return sb.ToString();

        }

        public string SaveThumb(string filename,int width,int heitht) {

            string rt;

            System.Drawing.Image picture = System.Drawing.Image.FromFile(PathPhysicalImage+filename);
            Bitmap result = new Bitmap(width, heitht);
            Graphics g = Graphics.FromImage(result);
            g.DrawImage(picture, 0, 0, width, heitht);
            result.Save(PathPhysicalImage+"thum_"+filename);

            g.Dispose();
            result.Dispose();
            picture.Dispose();

            rt = "thum_" + filename;

            return rt;
        
        }
    }
}
